<?php

class Bootstrap extends Yaf_Bootstrap_Abstract{

    public function _initCore() {
        ini_set('yaf.library', LIB_PATH);
    }

    // 添加三种路由，分别为 rewrite, rewrite_category, regex
    public function _initRoute() {
        $router = Yaf_Dispatcher::getInstance()->getRouter();

        // rewrite
        $route = new Yaf_Route_Rewrite(
            '/article/detail/:articleID',
            [
                'controller' => 'article',
                'action'     => 'detail',
            ]
        );

        $router->addRoute('rewrite', $route);

        // rewrite_category
        $route = new Yaf_Route_Rewrite(
            '/article/detail/:categoryID/:articleID',
            [
                'controller' => 'article',
                'action'     => 'detail',
            ]
        );

        $router->addRoute('rewrite_category', $route);

        // regex
        $route = new Yaf_Route_Regex(
            '#article/([0-9]+).html#',
            ['controller' => 'article', 'action' => 'detail'],
            [1 => 'articleID']
        );

        $router->addRoute('regex', $route);
    }

    public function _initRedis() {
        if(extension_loaded('Redis')){
            $config = Yaf_Application::app()->getConfig();
            $host  = $config['redis_host'];
            $port  = $config['redis_port'];
            $redis = new Redis();
            $redis->connect($host, $port);
            $redis->select(2);
            Yaf_Registry::set('redis', $redis);
        }
    }

//    public function _initLog() {
//        $str = date('Y-m-d').'===============================LOG==============================='.date('H:i:s');
//        Log::write($str,'info');
//        if(PHP_SAPI != 'cli'){
//            Log::write($_REQUEST,'request');
//        }
//    }
}
